package com.test.hTest;

import com.test.pojo.one2one.foreign.Department;
import com.test.pojo.one2one.foreign.Manager;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

public class crud {

    private SessionFactory sessionFactory;
    private Session session;
    private Transaction transaction;

    @Before
    public void before(){

        Configuration configuration = new Configuration().configure();
        //hibernate5以后
        configuration.addClass(Department.class);
        configuration.addClass(Manager.class);
        ServiceRegistry registry = new StandardServiceRegistryBuilder().applySettings(configuration.getProperties()).build();
        sessionFactory = configuration.buildSessionFactory(registry);
        session = sessionFactory.openSession();
        transaction = session.beginTransaction();
    }

    @After
    public void after(){
        transaction.commit();
        session.close();//关闭会话
        sessionFactory.close();//关闭会话工厂
    }

    @Test
    public void test1() {
      /*  User user = new User("admin2","123",23);
        session.save(user);*/

       //更新
        /*User user = new User();
        user.setId(3);
        user.setUsername("东方红");
        user.setPassword("111");
        user.setAge(44);

        session.update(user);*/
        /*User user = new User();
        user.setId(3);
        session.delete(user);*/

        //单个查询
        //User user = session.get(User.class,2);
        //System.out.println(user.getUsername());

      /*  User user = session.load(User.class,2);
        System.out.println(user.getUsername());*/

      //多条查询


       /* NativeQuery<User> query = session.createNativeQuery("select * from User",User.class);

        List<User> list =  query.getResultList();

        for(User u : list){
            System.out.println(u.getUsername());
        }*/
       //saveorupdate

       /* User user = new User();
        user.setId(7);
        user.setUsername("test");
        user.setPassword("qqqq");
        user.setAge(45);
        session.saveOrUpdate(user);*/

       /* for (int i = 0; i < 100; i++) {
            User user = new User();
            user.setUsername("" + i);
            if(i%20 == 0)
            {
                session.flush();
                session.clear();
            }
        }*/
    }

    @Test
    public void test2(){

        //补充表
       /* User user = new User();
        user.setUsername("test34");
        user.setPassword("1111");
        Userdetail userdetail = new Userdetail();
        userdetail.setAge(20);
        userdetail.setEmail("aaa@qq.com");
        userdetail.setMobilePhone("10086");
        user.setUserdetail(userdetail);

        session.save(user);*/


    }

    @Test
    public void test3(){

        /*Manager manager = new Manager();
        manager.setName("经理");
        Department department = new Department();
        department.setName("开发部");
        manager.setDepartment(department);
        session.save(manager);*/

       /* Department department = new Department();
        department.setName("开发部");
        Manager manager = new Manager();
        manager.setId(3);
        department.setManager(manager);
        session.save(department);*/



    }

}
